library(igraph)
library(data.table)
library(tidyverse)
library(scales)
library(ggrepel)

## To run network analysis, to only produce figure skip.

this.el <-
  data.table::fread("data/processed/pwdprotect_edgelist_user.csv.gz")


affordance_measures.df <- 
  data.frame(platform = character(),
             reciprocity = numeric(),
             sd = numeric(),
             transitivity = numeric(),
             mean_distance = numeric())

for (this_platform in c("fo", "mu", "bl", "fa")) {
  
  print(this_platform)
  
  this.g <- 
    this.el %>%
    dplyr::filter(platform == this_platform) %>%
    igraph::graph_from_data_frame()
  
  affordance_measures.df <- 
    affordance_measures.df %>%
    dplyr::bind_rows(data.frame(
      platform = this_platform,
      reciprocity = 
        igraph::reciprocity(this.g),
      sd = 
        sd(igraph::degree(this.g, mode = "in")),
      transitivity = 
        igraph::transitivity(this.g, type = "global"),
      mean_distance = 
        igraph::mean_distance(igraph::simplify(igraph::as.undirected(this.g)))))
  
}

data.table::fwrite(affordance_measures.df, 
                   file = "data/processed/affordance_measures.csv")

## To only produce figure

affordance_measures.df <- 
  data.table::fread("data/processed/affordance_measures.csv")

# Decode platform codes
affordance_measures.df$platform <- 
  recode(affordance_measures.df$platform, 
                      "bl" = "Blog", 
                      "fa" = "Facebook", 
                      "fo" = "Forum", 
                      "mu" = "Meetup")

long.df <- 
  affordance_measures.df %>%
  pivot_longer(-platform, names_to = "Metric", values_to = "Value") %>%
  mutate(
    Scale = case_when(
      Metric %in% c("sd", "mean_distance") ~ "Global",
      TRUE ~ "Local"
    ),
    Affordance = recode(Metric,
                        "reciprocity" = "Visibility",
                        "sd" = "Visibility",
                        "transitivity" = "Associability",
                        "mean_distance" = "Associability"),
    Metric = recode(Metric,
                    "reciprocity" = "Link reciprocity ratio",
                    "sd" = "SD of in-degree distribution",
                    "transitivity" = "Link transitivity ratio",
                    "mean_distance" = "Mean node distance"),

  ) %>%
  dplyr::filter(
    !(Affordance == "Visibility" & platform == "Meetup")
  )



long.df <- 
  long.df %>%
  dplyr::group_by(Scale, Metric, Affordance) %>%
  dplyr::mutate(Value_norm = scales::rescale(Value),
                Value_norm_inv = scales::rescale(Value, to = c(1, 0)))

long.df$Value_norm[long.df$Metric == "Mean node distance"] <-
  long.df$Value_norm_inv[long.df$Metric == "Mean node distance"]

map(c('output/figures/figure_2.eps', 'output/figures/figure_2.png'), 
    ~ ggsave(.x, plot =
               ggplot(long.df, aes(x = platform, y = Value_norm)) +
               geom_point() +
               geom_text_repel(aes(label = round(Value, 6))) +
               geom_label(aes(label = Metric, y = 1.5, x = "Facebook")) +
               facet_grid(Scale ~ Affordance) +
               theme_bw() +
               labs(x = NULL, y = "<- weaker   affordance  stronger ->",
                    caption = "Meetup visibility measures are unavailable as we collected no visibility proxy metrics."),
             width = 29, height = 14, units = 'cm'
    )
)


